﻿using OestsCommon.Model;
using OestsDataBase;
using OestsServer.Net;
using System.Collections.Generic;

namespace OestsServer.Managers
{
    /// <summary>
    /// 房间管理器
    /// </summary>
    public static class RoomManager
    {

        #region 管理房间的静态方法

        public static List<Room> roomList = new List<Room>();


        //添加一个房间
        public static void AddRoom(Room room)
        {
            if (roomList == null) roomList = new List<Room>();
            roomList.Add(room);
        }

        public static Room AddRoom(ProjectType pt, int roomId, string roomName, string roomPwd, TaskModel task)
        {
            //补充task的数据完整性，如添加任务的时间等
            task = TaskDAL.GetTaskById((int)pt, task.Id);
            Room room = new Room
            {
                ProType = pt,
                RoomId = roomId,
                RoomName = roomName,
                RoomPassword = roomPwd,
                Task = task
            };

            AddRoom(room);
            return room;
        }

        public static void RemoveRoom(Room room)
        {
            roomList.Remove(room);
        }
        public static void RemoveRoom(int roomId)
        {
            for (int i = 0; i < roomList.Count; i++)
            {
                if (roomList[i].RoomId == roomId)
                {
                    RemoveRoom(roomList[i]);
                    break;
                }

            }
        }

        public static void DeleteRoom(int roomId)
        {
            lock (roomList)
            {
                foreach (var item in roomList)
                {
                    if (item.RoomId == roomId)
                    {
                        if (item.Members != null)
                        {
                            foreach (var m in item.Members)
                            {
                                Student s = ServerController.Instance.UnityServerListener.ClientManager.GetStudentById(m.MemberId);
                                if (s != null)
                                    s._room = null;
                            }
                        }

                        roomList.Remove(item);
                        break;
                    }
                }
            }

        }

        public static int GetTaskIdByRoomId(int roomId)
        {
            foreach (var item in roomList)
            {
                if (item.RoomId == roomId)
                {
                    return item.Task.Id;
                }
            }
            return 0;
        }


        /// <summary>
        /// 删除一个房间成员
        /// </summary>
        /// <param name="client"></param>
        /// <param name="roomId"></param>
        /// <param name="memberId"></param>
        public static void DelMember(ProjectType type, int roomId, int memberId)
        {
            foreach (var item in roomList)
            {
                if (item.ProType == type && item.RoomId == roomId)
                {
                    item.DelMember(memberId);
                    break;
                }
            }
        }



        /// <summary>
        /// 检查房间名是否存在
        /// </summary>
        /// <param name="roomName"></param>
        /// <returns></returns>
        public static bool CheckRoomIsExist(string roomName)
        {
            if (roomList == null || roomList.Count == 0) return false;

            foreach (Room room in roomList)
            {
                if (room.RoomName == roomName)
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 获取特定客户端类型,特定任务的的房间列表
        /// </summary>
        /// <param name="client"></param>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public static List<Room> GetRoomList(ProjectType proType, int taskId)
        {
            List<Room> rlist = new List<Room>();
            foreach (var r in roomList)
            {
                if (r.ProType == proType && r.Task.Id == taskId && !r.InGame)
                {
                    rlist.Add(r);
                }
            }
            return rlist;
        }

        /// <summary>
        /// 获取特定客户端类型的所有房间
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static List<Room> GetRoomList(ProjectType type)
        {
            List<Room> rlist = new List<Room>();
            foreach (var r in roomList)
            {
                if (r.ProType == type && !r.InGame)
                {
                    rlist.Add(r);
                }
            }
            return rlist;
        }


        //public Room GetRoomByClient(ClientChannel client)
        //{
        //    //for (int i = 0; i < RoomList.Count; i++)
        //    //{
        //    //    if (RoomList[i].StudentTemps.Contains(client.User.DBid))
        //    //        return RoomList[i];
        //    //}
        //    return null;
        //}
        public static Room GetRoomById(int roomID)
        {
            if (roomList == null) return null;
            for (int i = 0; i < roomList.Count; i++)
            {
                if (roomList[i].RoomId == roomID)
                    return roomList[i];
            }
            return null;
        }

        /// <summary>
        /// 取消角色扮演，并且移除房间
        /// </summary>
        /// <param name="roomID"></param>
        /// <param name="memberID"></param>
        /// <returns></returns>
        public static Room RelieveRole(int roomID, int memberID)
        {
            if (roomList == null) return null;
            for (int i = 0; i < roomList.Count; i++)
            {
                if (roomList[i].RoomId == roomID)
                {
                    if (roomList[i].Members == null) return null; ;
                    foreach (var item in roomList[i].Members)
                    {
                        if (item.MemberId == memberID)
                        {
                            roomList[i].Members.Remove(item);
                            return roomList[i];
                        }
                    }
                    return null;
                }
            }
            return null;
        }


        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="roomId"></param>
        /// <param name="mId"></param>
        /// <returns></returns>
        public static Room JoinRoom(int roomId, int mId)
        {
            foreach (var item in roomList)
            {
                if (item.RoomId == roomId)
                {
                    item.AddMember(mId);
                    return item;
                }
            }
            return null;
        }

        public static bool CheckIsTrainFinishInRoom(int roomId, int dBid)
        {

            foreach (Room room in roomList)
            {
                if (room.RoomId == roomId)
                {
                    if (room.Members != null && room.Members.Count != 0)
                    {
                        foreach (var item in room.Members)
                        {
                            if (item.MemberId == dBid)
                            {
                                item.IsFinish = true;
                                break;
                            }

                        }
                        return CheckIsTrainFinish(room);
                    }

                }
            }
            return false;

        }

        private static bool CheckIsTrainFinish(Room room)
        {
            if (room.Members != null && room.Members.Count != 0)
            {
                foreach (var item in room.Members)
                {
                    if (!item.IsFinish)
                        return false;

                }
            }
            return true;
        }

        /// <summary>
        /// 房间内有成员场景加载完毕，返回房间内所有成员的场景是否都加载完毕
        /// </summary>
        /// <param name="roomid"></param>
        /// <param name="postid"></param>
        /// <returns></returns>
        public static bool SceneLoadedInRoom(int roomid, int postid)
        {
            foreach (var room in roomList)
            {
                if (room.RoomId == roomid)
                {
                    foreach (var mbr in room.Members)
                    {
                        if (mbr.PostId == postid)
                        {
                            mbr.SceneLoaded = true;
                            return CheckIsAllMemberLoaded(room);
                        }
                    }
                    break;
                }
            }
            return false;
        }


        private static bool CheckIsAllMemberLoaded(Room room)
        {
            foreach (var item in room.Members)
            {
                if (!item.SceneLoaded)
                    return false;
            }
            return true;
        }
        #endregion
    }
}